AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel("models/Items/item_item_crate.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)

	local physics = self.Entity:GetPhysicsObject()
	if (physics:IsValid()) then
		physics:EnableMotion(true)
		physics:Wake()
	end

self.PropHealth = nil
self.CrateShow = false
self.object = nil
self.skin = nil
self.model = nil
self.weapon = nil
self.class = nil
self.odd = false
end

function ENT:RecieveInfo(Original_Object)
	OBJ = Original_Object.object
	self.PropHealth = Original_Object.health
	
	if OBJ == 1 then
		self.object = Original_Object.object
		self.skin = Original_Object.skin
		self.model = Original_Object.model
		self.weapon = nil
		self.class = "prop_physics"
		self.odd = false
	elseif OBJ == 2 then
		self.object = Original_Object.object
		self.skin = Original_Object.skin
		self.model = Original_Object.model
		self.weapon = Original_Object.weapon
		self.class = Original_Object.class
		if self.model == "models/combine_super_soldier.mdl" then
			self.odd = true
		elseif self.model == "models/combine_soldier_prisonguard.mdl" then
			self.odd = true
		elseif table.HasValue(Citizens, self.model) then
			self.odd = true
        elseif table.HasValue(Medics, self.model) then
            self.odd = true
		elseif table.HasValue(Rebels, self.model) then
			self.odd = true
		end
	elseif OBJ == 3 then
		self.object = Original_Object.object
		self.skin = Original_Object.skin
		self.model = Original_Object.model
		self.weapon = nil
		self.class = Original_Object.class
		self.odd = false
	elseif OBJ == 4 then
		self.object = Original_Object.object
		self.skin = Original_Object.skin
		self.model = Original_Object.model
		self.weapon = nil
		self.class = Original_Object.class
		self.odd = false
	end
end

function ENT:OnTakeDamage(Damage)
	self.PropHealth = self.PropHealth - Damage:GetDamage()

	if self.PropHealth <= 0 then
		self:RemoveIt()
	end
end

function ENT:RemoveIt()
	local ent = self.Entity
	local pos = ent:GetPos()
	local object = self.object
	local skin = self.skin
	local model = self.model
	local weapon = self.weapon
	local class = self.class
	local odd = self.odd

	self:Remove()
	Fake = ents.Create("prop_physics")
	Fake:SetModel("models/Items/item_item_crate.mdl")	
	Fake:SetPos(pos)
	Fake:Spawn()
	Fake:Fire("break","",0)
	
	if object == 1 then -- Prop
		local Prop = ents.Create("prop_physics")
		if skin != nil then
			Prop:SetSkin(skin)
		end
		Prop:SetModel(model)
		Prop:SetPos(pos + Vector(0, 0, 50))
		undo.Create("Prop")
			undo.AddEntity(Prop)
			undo.SetPlayer(self:GetOwner())
		undo.Finish()
		Prop:Spawn()
	elseif object == 2 then -- NPC
		local NPC = ents.Create(class)
		if skin != nil then
			NPC:SetSkin(skin)
		end
		NPC:SetModel(model)
		NPC:SetPos(pos + Vector(0, 0, 50))
		undo.Create("NPC")
			undo.AddEntity(NPC)
			undo.SetPlayer(self:GetOwner())
		undo.Finish()
		NPC:Spawn()
		if weapon != nil then
			NPC:Give(weapon)
		end
	elseif object == 3 then -- Weapon
		local Weapon = ents.Create(class)
		if skin != nil then
			Weapon:SetSkin(skin)
		end
		Weapon:SetModel(model)
		Weapon:SetPos(pos + Vector(0, 0, 50))
		undo.Create("Weapon")
			undo.AddEntity(Weapon)
			undo.SetPlayer(self:GetOwner())
		undo.Finish()
		Weapon:Spawn()
	elseif object == 4 then -- Entity
		local Entity = ents.Create(class)
		if skin != nil then
			Entity:SetSkin(skin)
		end
		Entity:SetModel(model)
		Entity:SetPos(pos + Vector(0, 0, 50))
		undo.Create("Entity")
			undo.AddEntity(Entity)
			undo.SetPlayer(self:GetOwner())
		undo.Finish()
		Entity:Spawn()
	end
	--[[if self.NPCCrate == true then
		NPC = ents.Create(NPC)
		if NPCModell != nil then
		NPC:SetModel(NPCModell)
		end
		NPC:SetPos(Pos)
		NPC:Activate()
		NPC:Spawn()
		NPC:SetCollisionGroup(3)
	else
		OriginalProp = ents.Create("prop_physics")
		OriginalProp:SetModel(OriginalModel)
		OriginalProp:SetPos(self:GetPos() + Vector(0, 0, 50))
		OriginalProp:Spawn()
	end--]]
end

function ENT:Use(activator,caller)
	--[[if Crate_Info.CrateShow then
		Crate_Info.CrateShow = false
	else
		Crate_Info.CrateShow = true
	end--]]
end

function ENT:Think()
end

NPCA = {
"models/gman.mdl",
"models/crow.mdl",
"models/Monk.mdl",
"models/pigeon.mdl",
"models/seagull.mdl",
"models/combine_scanner.mdl",
"models/combine_super_soldier.mdl",
"models/combine_soldier.mdl",
"models/hunter.mdl",
"models/manhack.mdl",
"models/combine_soldier_prisonguard.mdl",
"models/combine_turrets/floor_turret.mdl",
"models/roller.mdl",
"models/roller_spikes.mdl",
"models/alyx.mdl",
"models/barney.mdl",
"models/Humans/Group01/male_03.mdl",
"models/dog.mdl",
"models/kleiner.mdl",
"models/magnusson.mdl",
"models/eli.mdl",
"models/breen.mdl",
"models/mossman.mdl",
"models/police.mdl",
"models/vortigaunt.mdl",




--Rebels
"models/Humans/Group03/male_03.mdl",
"models/Humans/Group03/female_04.mdl",
"models/Humans/Group03/female_01.mdl",
"models/Humans/Group03/male_09.mdl",
"models/Humans/Group03/male_01.mdl",
"models/Humans/Group03/female_02.mdl",
"models/Humans/Group03/female_04.mdl",
"models/Humans/Group03/male_04.mdl",
"models/Humans/Group03/female_06.mdl",
"models/Humans/Group03/male_02.mdl",
"models/Humans/Group03/male_02.mdl",
"models/Humans/Group03/male_07.mdl",
"models/Humans/Group03/male_08.mdl",

--Medics
"models/Humans/Group03m/male_09.mdl",
"models/Humans/Group03m/male_04.mdl",
"models/Humans/Group03m/male_08.mdl",
"models/Humans/Group03m/female_04.mdl",
"models/Humans/Group03m/male_03.mdl",
"models/Humans/Group03m/female_03.mdl",
"models/Humans/Group03m/male_07.mdl",
"models/Humans/Group03m/female_02.mdl",
"models/Humans/Group03m/female_06.mdl",
"models/Humans/Group03m/male_06.mdl",
"models/Humans/Group03m/male_01.mdl",
"models/Humans/Group03m/male_05.mdl",
"models/Humans/Group03m/male_02.mdl",
"models/Humans/Group03m/female_01.mdl",
"models/Humans/Group03m/female_07.mdl",

--Citizens
"models/Humans/Group01/male_02.mdl",
"models/Humans/Group01/female_06.mdl", 
"models/Humans/Group01/female_07.mdl", 
"models/Humans/Group01/male_08.mdl", 
"models/Humans/Group01/female_01.mdl", 
"models/Humans/Group01/male_01.mdl", 
"models/Humans/Group01/male_09.mdl", 
"models/Humans/Group01/female_04.mdl", 
"models/Humans/Group01/male_05.mdl", 
"models/Humans/Group01/male_03.mdl", 
"models/Humans/Group01/male_02.mdl", 
"models/Humans/Group01/male_06.mdl", 
"models/Humans/Group01/female_02.mdl", 
"models/Humans/Group01/male_07.mdl", 
"models/Humans/Group01/male_04.mdl", 
"models/Humans/Group01/female_03.mdl"
}

Citizens = {
"models/Humans/Group01/male_02.mdl",
"models/Humans/Group01/female_06.mdl", 
"models/Humans/Group01/female_07.mdl", 
"models/Humans/Group01/male_08.mdl", 
"models/Humans/Group01/female_01.mdl", 
"models/Humans/Group01/male_01.mdl", 
"models/Humans/Group01/male_09.mdl", 
"models/Humans/Group01/female_04.mdl", 
"models/Humans/Group01/male_05.mdl", 
"models/Humans/Group01/male_03.mdl", 
"models/Humans/Group01/male_02.mdl", 
"models/Humans/Group01/male_06.mdl", 
"models/Humans/Group01/female_02.mdl", 
"models/Humans/Group01/male_07.mdl", 
"models/Humans/Group01/male_04.mdl", 
"models/Humans/Group01/female_03.mdl"
}

Medics = {
"models/Humans/Group03m/male_09.mdl",
"models/Humans/Group03m/male_04.mdl",
"models/Humans/Group03m/male_08.mdl",
"models/Humans/Group03m/female_04.mdl",
"models/Humans/Group03m/male_03.mdl",
"models/Humans/Group03m/female_03.mdl",
"models/Humans/Group03m/male_07.mdl",
"models/Humans/Group03m/female_02.mdl",
"models/Humans/Group03m/female_06.mdl",
"models/Humans/Group03m/male_06.mdl",
"models/Humans/Group03m/male_01.mdl",
"models/Humans/Group03m/male_05.mdl",
"models/Humans/Group03m/male_02.mdl",
"models/Humans/Group03m/female_01.mdl",
"models/Humans/Group03m/female_07.mdl"
}

Rebels = {
"models/Humans/Group03/male_03.mdl",
"models/Humans/Group03/female_04.mdl",
"models/Humans/Group03/female_01.mdl",
"models/Humans/Group03/male_09.mdl",
"models/Humans/Group03/male_01.mdl",
"models/Humans/Group03/female_02.mdl",
"models/Humans/Group03/female_04.mdl",
"models/Humans/Group03/male_04.mdl",
"models/Humans/Group03/female_06.mdl",
"models/Humans/Group03/male_02.mdl",
"models/Humans/Group03/male_02.mdl",
"models/Humans/Group03/male_07.mdl",
"models/Humans/Group03/male_08.mdl"
}

BadNPC = {
"models/Hunter.mdl"
}